package com.haoji.upload.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Map;

/**
 * @program: upload
 * @description:
 * @author: xiaolei
 * @create: 2021-04-15 12:40
 **/
@RestController
@RequestMapping("/file")
public class FileController {


    @PostMapping("/upload")
    public String uploadImgFile(@RequestParam("file") MultipartFile file) {
        String name = file.getOriginalFilename();
        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            inputStream = file.getInputStream();
            File newFile = new File("D:\\test_file\\" + name);
            fileOutputStream = new FileOutputStream(newFile);
            byte[] b = new byte[1024*8];
            int length;
            while ((length = inputStream.read(b)) > 0) {
                fileOutputStream.write(b, 0, length);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "上传失败";
        } finally {
            try {
                fileOutputStream.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "上传成功";
    }


}
